const axios = require('axios');
const cheerio = require('cheerio');
const { subDays, parse } = require('date-fns');
const cloud = require('wx-server-sdk');

// 初始化云开发环境
cloud.init({
  env: 'lvtengweishi-4gvg0d2h59537d91',
  throwOnCode: false
});

// 初始化数据库
const db = cloud.database();
const newsCollection = db.collection('news');

// 配置信息 - 爬取目标网站列表（与原代码一致）
const WEBSITES = {
  "新疆维吾尔自治区农业农村厅_农业农村动态": {
    baseUrl: "https://nynct.xinjiang.gov.cn/xjnynct/c113576/",
    listSelector: "ul.commonlist-right-ul > li",
    titleSelector: "p.commonlist-content-title",
    dateSelector: "p.commonlist-content-meta span:first-child",
    contentSelector: "p.commonlist-content-text",
    pageUrl: (page) => page > 0 ? `list_${page}.shtml` : "list.shtml",
    totalPages: 100
  },
  "新疆维吾尔自治区农业农村厅_业务工作": {
    baseUrl: "https://nynct.xinjiang.gov.cn/xjnynct/c113559/",
    listSelector: "ul.commonlist-right-ul > li",
    titleSelector: "p.commonlist-content-title",
    dateSelector: "p.commonlist-content-meta span:first-child",
    contentSelector: "p.commonlist-content-text",
    pageUrl: (page) => page > 0 ? `list_noleft_${page}.shtml` : "list_noleft.shtml",
    totalPages: 27
  },
  "中国农业农村信息网_农业新闻": {
    baseUrl: "http://www.agri.cn/zx/nyyw/",
    listSelector: "div.nxw_list > ul.nxw_list_ul > li.nxw_list_li",
    titleSelector: "p.con_tit > a",
    dateSelector: "div.con_date > span.con_date_span",
    contentSelector: "p.con_text.zwnr",
    pageUrl: (page) => page > 0 ? `index_${page}.htm` : "",
    totalPages: 50
  },
  "中国农业农村信息网_全国信息联播": {
    baseUrl: "http://www.agri.cn/zx/xxlb/",
    listSelector: "div.nxw_list > ul.nxw_list_ul > li.nxw_list_li",
    titleSelector: "p.con_tit > a",
    dateSelector: "div.con_date > span.con_date_span",
    contentSelector: "p.con_text.zwnr",
    pageUrl: (page) => page > 0 ? `index_${page}.htm` : "",
    totalPages: 50
  },
  "中国农业农村信息网_通知公告": {
    baseUrl: "http://www.agri.cn/zx/hxgg/",
    listSelector: "div.nxw_list > ul.nxw_list_ul > li.nxw_list_li",
    titleSelector: "p.con_tit > a",
    dateSelector: "div.con_date > span.con_date_span",
    contentSelector: "p.con_text.zwnr",
    pageUrl: (page) => page > 0 ? `index_${page}.htm` : "",
    totalPages: 50
  },
  "中国农业农村信息网_最新发布": {
    baseUrl: "http://www.agri.cn/zx/zxfb/",
    listSelector: "div.nxw_list > ul.nxw_list_ul > li.nxw_list_li",
    titleSelector: "p.con_tit > a",
    dateSelector: "div.con_date > span.con_date_span",
    contentSelector: "p.con_text.zwnr",
    pageUrl: (page) => page > 0 ? `index_${page}.htm` : "",
    totalPages: 50
  },
  "云南省农业农村厅_云农快讯": {
    baseUrl: "https://nync.yn.gov.cn/html/yunnongkuanxun-new/",
    listSelector: "ul.zxwj-list-ul > li",
    titleSelector: "span:first-of-type > a",
    dateSelector: "span:nth-of-type(2)",
    contentSelector: "",  // 占位符，实际无摘要
    pageUrl: (page) => page > 0 ? `${page}.html` : "index.html",
    totalPages: 50
  },
  "四川省农业农村厅_市县联播": {
    baseUrl: "https://nynct.sc.gov.cn/nynct/c100632/",
    listSelector: "ul.artcleList > li",
    titleSelector: "a",
    dateSelector: "span:first-of-type",
    contentSelector: "",  // 占位符，实际无摘要
    pageUrl: (page) => page > 0 ? `nav_list_${page}.shtml` : "nav_list.shtml",
    totalPages: 50
  }
};

// 请求头
const headers = {
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
};

// 日期过滤：只保留1天内的数据（与原代码一致）
function isWithinMonth(dateStr) {
  try {
    let newsDate;
    const dateTimeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
    if (dateTimeRegex.test(dateStr)) {
      newsDate = parse(dateStr, 'yyyy-MM-dd HH:mm', new Date());
    } else {
      newsDate = parse(dateStr, 'yyyy-MM-dd', new Date());
    }
    const oneMonthAgo = subDays(new Date(), 1);
    return newsDate >= oneMonthAgo;
  } catch (error) {
    console.error("日期解析失败:", dateStr, error.message);
    return false;
  }
}

// 主爬取函数：获取新闻列表（与原代码一致）
async function crawl() {
  const allResults = [];
  for (const [siteName, config] of Object.entries(WEBSITES)) {
    console.log(`开始爬取 ${siteName}...`);
    let outOfTimeCount = 0;
    const totalPages = config.totalPages;

    for (let page = 0; page < totalPages; page++) {
      try {
        const pageUrl = new URL(config.pageUrl(page), config.baseUrl).href;
        console.log(`正在请求页面 URL: ${pageUrl}`);

        const response = await axios.get(pageUrl, { headers });
        const $ = cheerio.load(response.data);
        const newsList = $(config.listSelector);

        if (!newsList.length) {
          console.warn("未找到新闻列表元素");
          continue;
        }

        newsList.each((i, news) => {
          const $news = $(news);
          const titleElement = $news.find(config.titleSelector);
          const title = titleElement.text().trim() || "无标题";
          const dateElement = $news.find(config.dateSelector);
          const dateStr = dateElement.text().trim().split('：').pop()?.toString() || "无日期";

          let link = "无链接";
          const linkElement = siteName.includes("新疆") ? $news.find("a.commonlist-a") : titleElement;
          if (linkElement.length) {
            link = new URL(linkElement.attr("href"), config.baseUrl).href;
          }

          const content = siteName.includes("云南") || siteName.includes("四川") ? "无摘要" :
            $news.find(config.contentSelector).text().trim() || "无摘要";

          // 初步筛选：标题/摘要含葡萄相关关键词（为后续AI判断减少压力）
          if (isWithinMonth(dateStr) && (/葡萄|价格|优良品种|病虫害监测/.test(title) || /葡萄|价格|优良品种|病虫害监测/.test(content))) {
            allResults.push({
              title,
              date: dateStr,
              link,
              summary: content,
              section: siteName,
              createTime: db.serverDate()
            });
            outOfTimeCount = 0;
          } else {
            if (!isWithinMonth(dateStr)) outOfTimeCount++;
            if (outOfTimeCount >= 5) {
              console.log(`${siteName} 连续5条超时，停止爬取`);
              return;
            }
          }
        });

        if (outOfTimeCount >= 5) break;

      } catch (error) {
        console.error(`爬取 ${siteName} 第${page + 1}页失败:`, error.message);
      }

      console.log(`${siteName} 完成第${page + 1}/${totalPages}页`);
      await new Promise(resolve => setTimeout(resolve, 1000));
    }
  }
  return allResults;
}

// 调用Crawer_detail_content云函数获取详细内容（与原代码一致）
async function getDetailContent(url, siteName) {
  try {
    const result = await cloud.callFunction({
      name: 'Crawer_detail_content',
      data: { url, siteName }
    });
    if (result.result.code === 0 && result.result.data?.content) {
      return result.result.data.content;
    } else {
      console.warn(`详情获取失败（${url}）:`, result.result.message);
      return null;
    }
  } catch (error) {
    console.error(`调用Crawer_detail_content失败:`, error.message);
    return null;
  }
}

// 新增：调用AI判断内容是否与葡萄产业相关
async function isRelatedToGrape(fullContent, title) {
  try {
    // 构造判断提示词，明确要求AI返回结构化结果
    const prompt = `
      请判断以下新闻内容是否与葡萄产业（包括葡萄种植、品种、病虫害、价格、市场、技术等）相关。
      新闻标题：${title}
      新闻内容：${fullContent}
      判断标准：
      1. 直接提及葡萄（如“葡萄种植技术”“葡萄价格波动”）→ 相关；
      2.直接提及农产品批发价格（如“农产品批发价格200指数”）→ 相关；
      3. 仅提及其他农作物（如水稻、玉米）或不相关内容 → 不相关；
      4. 若内容涉及“优良品种”“病虫害监测”等通用术语，但未明确关联葡萄 → 相关。
      请仅返回“相关”或“不相关”，不要添加其他内容。
    `;
    const result = await cloud.callFunction({
      name: 'BlueLM-70BAPI',
      data: { prompt }
    });
    // 提取AI返回结果（去除空格和换行）
    console.log("输出蓝心小V的AI判断结果："+result.result.data);
    const aiJudgment = (result.result.data || '').toString().trim().toLowerCase();
    console.log("输出相关性判断结果："+aiJudgment);
    return aiJudgment === '相关';
  } catch (error) {
    console.error(`葡萄相关性判断失败:`, error.message);
    return false; // 出错时默认判定为不相关
  }
}

// 调用BlueLM-70BAPI云函数生成简化摘要（新增相关性标注）
async function simplifyContent(fullContent, isGrapeRelated) {
  if (!fullContent) return "无有效内容。" + (isGrapeRelated ? "该内容和葡萄有关" : "该内容和葡萄无关");
  try {
    const prompt = `请将以下农业相关内容简化为100-200字的摘要，保留关键信息（如事件、数据、结论等）：${fullContent}`;
    console.log("输入给蓝心小V的提示词："+prompt);
    const result = await cloud.callFunction({
      name: 'BlueLM-70BAPI',
      data: { prompt }
    });
    if (result.result.data && typeof result.result.data === 'string') {
      // 在摘要末尾添加相关性标注
      return `${result.result.data} ${isGrapeRelated ? "该内容和葡萄有关" : "该内容和葡萄无关"}`;
    } else {
      console.warn(`AI简化失败:`, result.result.data?.errMsg || '未知错误');
      return `${fullContent.substring(0, 200)}... ${isGrapeRelated ? "该内容和葡萄有关" : "该内容和葡萄无关"}`;
    }
  } catch (error) {
    console.error(`调用BlueLM-70BAPI失败:`, error.message);
    return `${fullContent.substring(0, 200)}... ${isGrapeRelated ? "该内容和葡萄有关" : "该内容和葡萄无关"}`;
  }
}

// 存储到数据库（与原代码一致）
async function saveToDatabase(results) {
  try {
    const batchSize = 50;
    const total = results.length;
    const tasks = [];

    for (let i = 0; i < total; i += batchSize) {
      const batch = results.slice(i, i + batchSize);
      tasks.push(newsCollection.add({ data: batch }));
    }

    await Promise.all(tasks);
    console.log('数据存储完成! 共存储', total, '条记录');
    return { success: true, count: total };
  } catch (error) {
    console.error("存储数据库时发生错误:", error.message);
    return { success: false, error: error.message };
  }
}

// 云函数入口：整合全流程（新增相关性判断逻辑）
exports.main = async (event, context) => {
  try {
    console.log('云函数触发，开始爬取...');
    // 步骤1：爬取新闻列表
    const rawResults = await crawl();
    console.log('爬取完成，原始结果数量:', rawResults.length);

    if (rawResults.length === 0) {
      return { code: 1, message: "未爬取到任何结果", data: null };
    }

    // 步骤2：逐条处理（获取详情→判断相关性→AI简化→过滤无关数据）
    const processedResults = [];
    for (const item of rawResults) {
      console.log(`处理新闻: ${item.title}（${item.link}）`);
      
      // 2.1 获取详情内容
      const fullContent = await getDetailContent(item.link, item.section);
      if (!fullContent) {
        console.warn(`无详情内容，跳过处理: ${item.title}`);
        continue; // 无详情时直接跳过（避免误判）
      }else{
        console.log("获得的新闻的详细内容："+fullContent);
      }

      // 2.2 判断是否与葡萄产业相关
      const isGrapeRelated = await isRelatedToGrape(fullContent, item.title);
      console.log(`相关性判断: ${item.title} → ${isGrapeRelated ? "相关" : "不相关"}`);

      // 2.3 若不相关，从数组中删除（不加入结果）
      if (!isGrapeRelated) {
        console.log(`过滤无关数据: ${item.title}`);
        continue;
      }

      // 2.4 若相关，生成带标注的简化摘要
      const simplifiedSummary = await simplifyContent(fullContent, isGrapeRelated);

      // 2.5 加入结果数组
      processedResults.push({
        ...item,
        summary: simplifiedSummary
      });

      // 控制调用频率
      await new Promise(resolve => setTimeout(resolve, 1500)); // 延长延迟，避免AI接口压力
    }

    // 步骤3：存储处理后的结果（仅保留相关数据）
    console.log(`过滤后保留的相关数据数量: ${processedResults.length}`);
    if (processedResults.length === 0) {
      return { code: 1, message: "未找到与葡萄产业相关的有效数据", data: null };
    }

    const saveResult = await saveToDatabase(processedResults);
    if (saveResult.success) {
      return {
        code: 0,
        message: 'success',
        data: { count: saveResult.count }
      };
    } else {
      throw new Error(saveResult.error);
    }

  } catch (error) {
    console.error('全局错误:', error.message);
    return { code: -1, message: error.message, data: null };
  }
};